﻿// Project: WebPage, File: HTMLTextWebPage.cs
// Namespace: WebPage, Class: HTMLTextWebPage
// Path: G:\Secure Project\WebCrawlerProject\WebPage, Author: Radu-Mihail
// Code lines: 132, Size of file: 3.89 KB
// Creation date: 1/9/2009 12:11 AM
// Last modified: 1/11/2009 4:17 AM

#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using WebPage.Client;
#endregion


namespace WebPage
{
    /// <summary>
    /// Web page
    /// </summary>
    public class HTMLTextWebPage : AbstractWebPage
    {
        #region Private fields
        private const string LINK_PATTERN = @"(href|HREF)[ ]*=[ ]*[""'][^""'#>]+[""']";

        /// <summary>
        /// Link list
        /// </summary>
        private List<string> _linkList;

        /// <summary>
        /// _page content
        /// </summary>
        private string _pageContent;

        #endregion
        #region Properties

        /// <summary>
        /// Link list
        /// </summary>
        /// <returns>List containing the found links</returns>
        public override List<string> LinkList
        {
            get { return _linkList; } // get
        } // LinkList       


        #endregion
        #region Overriden methods
        public override object GetPageContent()
        {
            return _pageContent;
        }
        #endregion
        #region Constructor
        /// <summary>
        /// Create text web page
        /// </summary>
        /// <param name="downloadClient">Download client</param>
        public HTMLTextWebPage(ref MyWebClient downloadClient, bool revisit)
            : base(ref downloadClient, revisit)
        {
            try
            {
                _pageContent = Encoding.ASCII.GetString(_fileContent);
                ParsePage();
            } // try
            catch (Exception)
            {
                throw new Exception("Exceptie");
            } // catch (exc)
            // Else just download the page content

        } // TextWebPage(downloadClient)
        #endregion
        #region Static methods
        /// <summary>
        /// Normalize link
        /// </summary>
        /// <param name="webPageLink">Web page link</param>
        /// <param name="webPageLinkSource">Web page link source</param>
        /// <returns>The normalized link</returns>
        public static string NormalizeLink(string webPageLink, string webPageLinkSource)
        {
            if (webPageLink.StartsWith("http://") || webPageLink.StartsWith("ftp://") || webPageLink.StartsWith("https://"))
                return webPageLink;

            Uri test = new Uri(new Uri(webPageLinkSource), webPageLink);

            if (test.AbsoluteUri.StartsWith("http://") || test.AbsoluteUri.StartsWith("ftp://") || test.AbsoluteUri.StartsWith("https://"))
                 return test.AbsoluteUri;

            return null;
        } // NormalizeLink(webPageLink, webPageLinkSource)

        #endregion

        #region Methods


        /// <summary>
        /// Parse page
        /// </summary>
        private void ParsePage()
        {
            _linkList = new List<string>();
            Regex checkPage = new Regex(LINK_PATTERN, RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
            MatchCollection mc = checkPage.Matches(_pageContent);

            foreach (Match m in mc)
            {
                string newLink = HTMLTextWebPage.NormalizeLink(m.Value.Substring(m.Value.IndexOf('=') + 1).Trim('"', '\'', '#', ' ', '>'), _webLink);
                if (null != newLink)
                {
                    if(!(_linkList.Contains(newLink)))
                        _linkList.Add(newLink);
                } // if (null)
                 // if (null)
            } // foreach

        } // ParsePage()

        #endregion
    } // class TextWebPage
} // namespace WebPage
